package edu.hawaii.ics414;

import java.util.HashMap;
import java.util.Map;
import android.os.AsyncTask;
import android.util.Log;

/**
 * Gets the current amount of times a game has been favorited.
 * 
 * @author Matthew Mizumoto
 * 
 */
public class GetCurrentFavorites extends AsyncTask<String, Void, Integer> {

  @Override
  /*
   * Required by the AsyncTask class. Sends data to the appropriate php page for updates/queries
   * (non-Javadoc)
   * 
   * @params params A variable amount of Strings to send as form data.
   * 
   * @see android.os.AsyncTask#doInBackground(Params[])
   */
  protected Integer doInBackground(String... params) {
    String gameName = (params.length > 0) ? params[0] : "";

    Map<String, String> requestParams = new HashMap<String, String>();
    requestParams.put("gameName", gameName);
    String response =
        Ajax.send("http://ratethatgame.web44.net/ics414/get_current_favorite.php", requestParams);
    if (response != "") {
      try {
        int count = Integer.parseInt(response.trim());
        Log.wtf("", "TOTAL" + response);
        return count;
      }
      catch (NumberFormatException nfe) {
        Log.wtf("", "No Favorites");
        return 0;
      }

    }
    return 0;
  }
}
